<template>
	<view class="wrap">
		<view class="filter-box">
			<view class="b-item" v-for="(item, index) in groupList" :key="index"
				:class="{'f-active': index == groupIndex}" @click="onGroupSelect(index)">
				<view class="s-tit">{{item.name}}</view>
			</view>
		</view>
		<view class="order-list" v-if="orderList.length > 0">
			<view class="b-item" v-for="(item, index) in orderList" :key="index">
				<view class="b-head">
					<view class="s-order">订单编号：{{item.order_no}}</view>
					<view class="s-status" :class="stateStyle(item.status)">{{item.status_text}}</view>
				</view>
				<view class="b-goods" v-if="item.goods_list.length === 1">
					<view class="b-left">
						<image :src="item.goods_list[0].goods_cover" mode="aspectFill"></image>
					</view>
					<view class="b-main">
						<view class="b-title">{{item.goods_list[0].goods_name}}</view>
						<view class="b-spec">
							<view class="s-value">{{item.goods_list[0].goods_names}}</view>
						</view>
						<view class="b-fund">
							<view class="s-count">x{{item.goods_list[0].goods_count}}</view>
							<!-- <view class="s-worth">¥{{item.goods_list[0].goods_price}}</view> -->
							<view class="s-num">¥{{item.goods_list[0].payment}}</view>
						</view>
					</view>
				</view>
				<view class="b-goods-list" v-else-if="item.goods_list.length > 1">
					<view class="b-items">
						<view class="b-item" v-for="(sItem, sIndex) in item.goods_list" :key="sIndex">
							<image :src="sItem.goods_cover" mode="aspectFill"></image>
						</view>
					</view>
					<view class="b-state">
						<view class="s-payment">¥{{getTotalPrice(item.goods_list)}}</view>
						<view class="s-count">共{{getTotalCount(item.goods_list)}}件</view>
					</view>
				</view>
				<view class="b-foot">
					<view class="s-time">下单时间：{{timeFormat(item.created_at)}}</view>
					<view class="b-amount" v-if="item.payment_status == 1">
						<view class="s-tit">现付:</view>
						<view class="s-num">¥{{getLeftAmount(index)}}</view>
					</view>
					<view class="b-amount" v-else>
						<view class="s-tit">现付:</view>
						<view class="s-num">¥{{item.payment_rel}}</view>
					</view>
				</view>
				<view class="b-action">
					<view class="b-btn" @click="onRecordDetail(item.order_no)">订单详情</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" color="#888888"></load-more>
	</view>
</template>

<script>
	import {
		getOrderList
	} from '@/service/api/order.js'
	import Util from '@/common/util'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				hasLoad: false,
				refreshTime: 0,
				groupIndex: 0,
				groupList: [{
						name: '全部',
						value: 0
					},
					{
						name: '待支付',
						value: 1
					},
					{
						name: '已完成',
						value: 2
					},
					{
						name: '已取消',
						value: 3
					},
				],
				orderList: [],
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadOrderList(true)
		},
		onShow() {
			if (this.hasLoad) {
				const timestamp = Util.getTimestamp()
				if (timestamp - this.refreshTime >= 10) {
					this.refreshTime = timestamp
					this.loadOrderList(true)
				}
			}
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadOrderList();
			}
		},
		computed: {
			stateStyle() {
				return (status) => {
					return {
						'f-wait': status === 1,
						'f-done': status === 2,
						'f-cancel': status === 3,
					}
				}
			},
			getTotalPrice() {
				return (goodsList) => {
					let totalPrice = 0
					for (let key in goodsList) {
						totalPrice += parseFloat(goodsList[key].payment)
					}
					return totalPrice
				}
			},
			getTotalCount() {
				return (goodsList) => {
					let goodsCount = 0
					for (let key in goodsList) {
						goodsCount += parseInt(goodsList[key].goods_count)
					}
					return goodsCount
				}
			},
			getLeftAmount() {
				return (index) => {
					return (this.orderList[index].payment - this.orderList[index].balance).toFixed(2)
				}
			}
		},
		methods: {
			loadOrderList(refresh) {
				console.log('loadOrderList', refresh);
				if (refresh) {
					this.page = 1;
					this.orderList = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getOrderList(this.groupList[this.groupIndex].value, this.page, 10).then(([err, res]) => {
					console.log('getList', err, res)
					this.loadingType = -1
					this.hasLoad = true
					if (!err) {
						if (res.items.length > 0) {
							this.orderList = [...this.orderList, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无订单'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			},
			onGroupSelect(index) {
				this.groupIndex = index
				this.loadOrderList(true)
			},
			onRecordDetail(order_no) {
				uni.navigateTo({
					url: '/pages/order/detail?order_no=' + order_no
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {
		padding-bottom: 50upx;
	}

	.filter-box {
		padding: 30upx 50upx 0 50upx;
		background: #fff;
		// border-bottom: 1upx solid #eee;
		display: flex;
		align-items: center;

		.b-item {
			margin: 0 40upx;
			height: 80upx;
			line-height: 80upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30upx;
			padding-bottom: 6upx;

			&.f-active {
				padding-bottom: 0;
				border-bottom: 6upx solid $pq-active-color;

				.s-tit {
					// color: #DA5650;
				}
			}

			.s-tit {
				color: $pq-main-color;
			}
		}
	}

	.order-list {
		padding: 20upx;

		.b-item {
			margin-bottom: 30upx;
			width: 710upx;
			background: #fff;
			border-radius: 10upx;

			&:last-child {
				margin-bottom: 0;
			}

			.b-head {
				padding: 20upx 30upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1upx solid #eee;

				.s-order {
					color: #1A1A1A;
					font-size: 26upx;
				}

				.s-status {
					font-size: 28upx;

					&.f-wait {
						color: #FF1334;
					}

					&.f-done {
						color: #59CC20;
					}

					&.f-cancel {
						color: #1A1A1A;
					}
				}
			}

			.b-goods {
				padding: 30upx;
				display: flex;
				border-bottom: 1upx solid #eee;

				.b-left {
					width: 120upx;
					height: 120upx;

					image {
						width: 120upx;
						height: 120upx;
						border-radius: 10upx;
					}
				}

				.b-main {
					margin-left: 30upx;
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.b-title {
						font-size: 28upx;
						color: #333;
					}


					.b-spec {
						margin-top: 20upx;
						display: flex;
						align-items: center;

						.s-value {
							font-size: 24upx;
							color: #666;
						}
					}

					.b-fund {
						margin-top: 20upx;
						display: flex;
						align-items: baseline;

						.s-count {
							font-size: 26upx;
							color: #666;
						}

						.s-worth {
							margin-left: 20upx;
							font-size: 26upx;
							color: #999;
						}

						.s-num {
							margin-left: 30upx;
							font-size: 32upx;
							color: #FF9523;
              font-weight: bold;
						}
					}
				}
			}

			.b-goods-list {
				padding: 30upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1upx solid #eee;

				.b-items {
					display: flex;

					.b-item {
						margin-right: 20upx;
						width: 120upx;
						height: 120upx;

						image {
							width: 120upx;
							height: 120upx;
							border-radius: 10upx;
						}
					}
				}

				.b-state {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;

					.s-payment {
						font-size: 26upx;
						color: #FF9523;
            font-weight: bold;
					}

					.s-count {
						font-size: 24upx;
						color: #666;
					}
				}
			}

			.b-foot {
				padding: 20upx 30upx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.s-time {
					color: #1A1A1A;
					font-size: 26upx;
				}

				.b-amount {
					display: flex;
					align-items: center;

					.s-tit {
						color: #333;
						font-size: 28upx;
					}

					.s-num {
						margin-left: 20upx;
						color: #FF9523;
            font-weight: bold;
						font-size: 32upx;
					}
				}
			}


			.b-action {
				padding: 20upx 30upx 40upx 30upx;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.b-btn {
					margin-right: 30upx;
					padding: 0 30upx;
					border: 1upx solid #1A1A1A;
					color: #1A1A1A;
					font-size: 26upx;
					line-height: 60upx;
					height: 60upx;
					border-radius: 30upx;

					&:last-child {
						margin-right: 0;
					}
				}
			}
		}
	}
</style>